<template>
  <div class="promotion-page">
    <!-- 页面标题 -->
    <div class="page-header">
      <h1 class="title">欢迎来到在线教育平台</h1>
      <p class="subtitle">知识改变命运 · 学习成就未来</p>
    </div>

    <!-- 课程轮播 -->
    <div class="section">
      <h2 class="section-title">热门课程</h2>
      <div v-if="coursesLoading" class="loading">
        <el-skeleton :rows="3" animated />
      </div>
      <div v-else-if="coursesError" class="error">
        <el-alert :title="coursesError" type="error" show-icon />
        <el-button @click="fetchCourses" type="primary" class="retry-btn">重新加载</el-button>
      </div>
      <div v-else class="carousel-container">
        <el-carousel 
          :interval="4000" 
          type="card" 
          height="300px"
          indicator-position="outside"
        >
          <el-carousel-item v-for="course in courseList" :key="course.id">
            <div class="carousel-item course-item">
              <div class="item-image">
                <img 
                  v-if="course.picture" 
                  :src="getImageUrl(course.picture)" 
                  :alt="course.name"
                  @error="handleImageError"
                >
                <div v-else class="placeholder-image">
                  <el-icon><Document /></el-icon>
                </div>
              </div>
              <div class="item-content">
                <h3 class="item-title">{{ course.name }}</h3>
                <p class="item-description">{{ truncateText(course.content, 100) }}</p>
                <div class="item-meta">
                  <el-tag :type="course.status === 'active' ? 'success' : 'info'">
                    {{ course.status === 'active' ? '进行中' : '已结束' }}
                  </el-tag>
                  <span class="item-extra">课程ID: {{ course.extral_1 || 'N/A' }}</span>
                </div>
              </div>
            </div>
          </el-carousel-item>
        </el-carousel>
      </div>
    </div>

    <!-- 教师轮播 -->
    <div class="section">
      <h2 class="section-title">优秀教师</h2>
      <div v-if="teachersLoading" class="loading">
        <el-skeleton :rows="3" animated />
      </div>
      <div v-else-if="teachersError" class="error">
        <el-alert :title="teachersError" type="error" show-icon />
        <el-button @click="fetchTeachers" type="primary" class="retry-btn">重新加载</el-button>
      </div>
      <div v-else class="carousel-container">
        <el-carousel 
          :interval="5000" 
          height="250px"
          indicator-position="outside"
        >
          <el-carousel-item v-for="teacher in teacherList" :key="teacher.id">
            <div class="carousel-item teacher-item">
              <div class="teacher-avatar">
                <el-avatar :size="80" :src="teacher.avatar || ''">
                  {{ getInitials(teacher.name) }}
                </el-avatar>
              </div>
              <div class="teacher-content">
                <h3 class="teacher-name">{{ teacher.name }}</h3>
                <p class="teacher-info">
                  <el-icon><User /></el-icon>
                  用户ID: {{ teacher.id }}
                </p>
                <p class="teacher-courses">
                  <el-icon><Document /></el-icon>
                  课程数量: {{ teacher.courseCount }}
                </p>
                <div class="teacher-meta">
                  <el-tag type="primary">教师</el-tag>
                </div>
              </div>
            </div>
          </el-carousel-item>
        </el-carousel>
      </div>
    </div>
  </div>
</template>

<script setup>
import { ref, onMounted } from 'vue'
import { Document, User } from '@element-plus/icons-vue'
import http from '@/util/request'

// 课程相关数据
const courseList = ref([])
const coursesLoading = ref(true)
const coursesError = ref(null)

// 教师相关数据
const teacherList = ref([])
const teachersLoading = ref(true)
const teachersError = ref(null)

// 获取课程列表
const fetchCourses = async () => {
  try {
    coursesLoading.value = true
    coursesError.value = null
    
    const response = await http.get('/api/courseList')
    
    if (response.data?.code === 200) {
      // 只显示状态为active的课程
      courseList.value = response.data.data.filter(course => course.status === 'active')
    } else {
      coursesError.value = '获取课程列表失败'
    }
  } catch (error) {
    console.error('获取课程列表错误:', error)
    coursesError.value = '获取课程列表时发生错误'
  } finally {
    coursesLoading.value = false
  }
}

// 获取教师列表
const fetchTeachers = async () => {
  try {
    teachersLoading.value = true
    teachersError.value = null
    
    const response = await http.get('/api/courseList')
    
    if (response.data?.code === 200) {
      // 从课程列表中提取教师信息并去重
      const teachersMap = new Map()
      
      response.data.data.forEach(course => {
        if (course.user_id__name) {
          if (!teachersMap.has(course.user_id__id)) {
            teachersMap.set(course.user_id__id, {
              id: course.user_id__id,
              name: course.user_id__name,
              courseCount: 1
            })
          } else {
            teachersMap.get(course.user_id__id).courseCount++
          }
        }
      })
      
      teacherList.value = Array.from(teachersMap.values())
    } else {
      teachersError.value = '获取教师列表失败'
    }
  } catch (error) {
    console.error('获取教师列表错误:', error)
    teachersError.value = '获取教师列表时发生错误'
  } finally {
    teachersLoading.value = false
  }
}

// 获取图片URL
const getImageUrl = (filename) => {
  return `http://localhost:8000/uploads/${filename}`
}

// 处理图片加载错误
const handleImageError = (event) => {
  event.target.style.display = 'none'
}

// 截断文本
const truncateText = (text, length = 50) => {
  if (!text) return ''
  return text.length > length ? text.substring(0, length) + '...' : text
}

// 获取姓名首字母
const getInitials = (name) => {
  if (!name) return ''
  return name.charAt(0).toUpperCase()
}

// 组件挂载时获取数据
onMounted(() => {
  fetchCourses()
  fetchTeachers()
})
</script>

<style scoped>
.promotion-page {
  padding: 20px;
  max-width: 1200px;
  margin: 0 auto;
}

.page-header {
  text-align: center;
  margin-bottom: 40px;
  padding: 20px 0;
}

.title {
  font-size: 32px;
  color: #1856cd;
  margin-bottom: 10px;
}

.subtitle {
  font-size: 18px;
  color: #666;
}

.section {
  margin-bottom: 50px;
}

.section-title {
  text-align: center;
  font-size: 24px;
  color: #333;
  margin-bottom: 30px;
  position: relative;
}

.section-title::after {
  content: '';
  display: block;
  width: 60px;
  height: 3px;
  background: #1856cd;
  margin: 10px auto 0;
  border-radius: 2px;
}

.carousel-container {
  padding: 0 20px;
}

.carousel-item {
  height: 100%;
  display: flex;
  flex-direction: column;
  border-radius: 8px;
  overflow: hidden;
  box-shadow: 0 4px 12px rgba(0, 0, 0, 0.1);
  background: #fff;
}

.course-item {
  background: linear-gradient(135deg, #f5f7fa 0%, #e4edf9 100%);
}

.teacher-item {
  background: linear-gradient(135deg, #f0f9ff 0%, #e6f4ff 100%);
  flex-direction: row;
  align-items: center;
  padding: 20px;
}

.item-image {
  height: 150px;
  overflow: hidden;
  display: flex;
  align-items: center;
  justify-content: center;
  background-color: #f5f7fa;
}

.item-image img {
  width: 100%;
  height: 100%;
  object-fit: cover;
}

.placeholder-image {
  font-size: 48px;
  color: #c0c4cc;
}

.item-content {
  padding: 20px;
  flex: 1;
  display: flex;
  flex-direction: column;
}

.item-title {
  font-size: 18px;
  font-weight: bold;
  margin: 0 0 10px 0;
  color: #333;
}

.item-description {
  font-size: 14px;
  color: #666;
  margin: 0 0 15px 0;
  flex: 1;
}

.item-meta {
  display: flex;
  justify-content: space-between;
  align-items: center;
}

.teacher-avatar {
  margin-right: 30px;
}

.teacher-content {
  flex: 1;
}

.teacher-name {
  font-size: 20px;
  font-weight: bold;
  margin: 0 0 15px 0;
  color: #333;
}

.teacher-info,
.teacher-courses {
  font-size: 14px;
  color: #666;
  margin: 8px 0;
  display: flex;
  align-items: center;
}

.teacher-info .el-icon,
.teacher-courses .el-icon {
  margin-right: 8px;
  font-size: 16px;
}

.teacher-meta {
  margin-top: 15px;
}

.loading,
.error {
  padding: 40px 20px;
  text-align: center;
}

.retry-btn {
  margin-top: 20px;
}

@media (max-width: 768px) {
  .promotion-page {
    padding: 10px;
  }
  
  .title {
    font-size: 24px;
  }
  
  .subtitle {
    font-size: 16px;
  }
  
  .teacher-item {
    flex-direction: column;
    text-align: center;
  }
  
  .teacher-avatar {
    margin-right: 0;
    margin-bottom: 20px;
  }
  
  .carousel-container {
    padding: 0;
  }
}
</style>
